//
//  BiBaseViewController.m
//  CodoonBikeApp
//
//  Created by ChenBin on 14-3-28.
//  Copyright (c) 2014年 Nahan Cop. All rights reserved.
//

#define HUDVIEWTAG 1111

#import "BiBaseViewController.h"
#import "METoast.h"
#import "BiTipsView.h"
#import "BiAlertView.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
@interface BiBaseViewController ()<BiAlertViewDelegate>{
    ASIHTTPRequest *requestBase;
    ASIFormDataRequest *requestFormat;
}
@property (nonatomic, retain) NSTimer * tipsTimer;
@end

@implementation BiBaseViewController
@synthesize tipsTimer;

- (id)init
{
    self = [super init];
    if (self)
    {
        if(isIOS7)
        {
            _topMargin = 20;
        }
    }
    return self;
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    _naviBarView = [[BiNaviBarView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    [self.view addSubview:_naviBarView];
    [_naviBarView setHidden:NO];
    if (isIOS7)
    {
        [_naviBarView setFrame:CGRectMake(0, 0, _naviBarView.frame.size.width, 64)];
    }
    else
    {
        [_naviBarView setFrame:CGRectMake(0, -20, _naviBarView.frame.size.width, 64)];
    }
    //[_naviBarView release];
    

}

- (void)dealloc
{
    if (nil != self.tipsTimer)
    {
        [self.tipsTimer invalidate];
        self.tipsTimer = nil;
    }
    
    //[super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setNavBarWithHidden:(BOOL)hidden
{
    [_naviBarView setHidden:hidden];
    
    if (hidden)
    {
        [_naviBarView setFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)];
    }
    else
    {
        [self.view bringSubviewToFront:_naviBarView];
        
        if (isIOS7)
        {
            [_naviBarView setFrame:CGRectMake(0, 0, _naviBarView.frame.size.width, 64)];
        }
        else
        {
            [_naviBarView setFrame:CGRectMake(0, -20, _naviBarView.frame.size.width, 64)];
        }
    }
}

- (void)setNavBarTitle:(NSString *)title
{
    [_naviBarView setTitle:title];
}

- (void)setNavBarTitleImage:(UIImage*)image
{
    [_naviBarView setTitleImage:image];
}

- (void)setNavBackBtn
{
    [self setNavLeftButton:LOAD_IMAGE_WITH_CACHE(@"common/icon_main_back.png") target:self action:@selector(onNavBackBtnClick:)];
}

- (void)setNavLeftButton:(UIImage *)icon target:(id)target action:(SEL)action
{
    [_naviBarView setLeftButton:icon target:target action:action];
}
- (void)setNavRightButton:(UIImage *)icon target:(id)target action:(SEL)action
{
    [_naviBarView setRightButton:icon target:target action:action];
}
- (void)setNavRightButtonTitle:(NSString*)title target:(id)target action:(SEL)action
{
    [_naviBarView setRightButtonTitle:title target:target action:action];
}
- (void)removeNavRightBtn
{
    [_naviBarView setRightButtonTitle:@"" target:nil action:nil];
}

- (void)onNavBackBtnClick:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (int)viewTop
{
    return (_naviBarView.frame.origin.y + _naviBarView.frame.size.height);
}

- (void)addTestView
{
    UIView * testView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:testView];
}

- (void)showTipsView:(NSString*)tipText
{
    BiTipsView * tipsView = (BiTipsView *)[self.view viewWithTag:TIP_VIEW_TAG];
    if (nil == tipsView)
    {
        CGRect screenBounds = [UIScreen mainScreen].bounds;
        tipsView = [[BiTipsView alloc] initWithFrame:CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height)];
        [tipsView setTag:TIP_VIEW_TAG];
        [self.view addSubview:tipsView];
    }
    
    [tipsView setTipText:tipText];
    [self.view bringSubviewToFront:tipsView];
    
    self.tipsTimer = [NSTimer scheduledTimerWithTimeInterval:1.8 target:self selector:@selector(removeTipsView) userInfo:nil repeats:NO];
}

- (void)removeTipsView
{
    self.tipsTimer = nil;
    
    BiTipsView * tipsView = (BiTipsView *)[self.view viewWithTag:TIP_VIEW_TAG];
    if (nil != tipsView)
    {
        [tipsView removeFromSuperview];
    }
}

- (void)showMessage:(NSString *)message{
    
    [METoast resetToastAttribute];
    [METoast toastWithMessage:message];
}

- (void)showAlertView:(NSString*)tipText
{
    BiAlertView * alertView = (BiAlertView *)[self.view viewWithTag:TIP_VIEW_TAG];
    if (nil == alertView)
    {
        CGRect screenBounds = [UIScreen mainScreen].bounds;
        alertView = [[BiAlertView alloc] initWithFrame:CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height)];
        [alertView setTag:ALERT_VIEW_TAG];
        [self.view addSubview:alertView];
        alertView.delegate = self;
    }
    
    [alertView setTipText:tipText];
    [self.view bringSubviewToFront:alertView];
}

- (void)onAlertViewOkBtnClick
{
    
}

- (BOOL)isConnectToNetwork
{
    if (![BiUtil isConnectToNetwork])
    {
        [self showTipsView:@"亲，你的网络不给力。"];
        return NO;
    }
    
    return YES;
}

- (void)asyshowHUDView:(NSString *)title CurrentView:(UIViewController *)viewController
{
    Bi_MBProgressHUD *tagview = (Bi_MBProgressHUD *)[self.view viewWithTag:HUDVIEWTAG];
    if (tagview)
    {
        tagview.labelText = title;
        return;
    }
    //    CGFloat hudOrgin = IOS7_OR_LATER ? 64 : 44;
    HUD = [[Bi_MBProgressHUD alloc] initWithView:self.view];
    HUD.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
    HUD.animationType = MBProgressHUDAnimationZoom;
    HUD.tag = HUDVIEWTAG;
    [self.view addSubview:HUD];
    [HUD bringSubviewToFront:self.view];
    HUD.labelText = title;
    
    [HUD show:YES];
}

- (void)removeHUDView:(UIViewController *)viewcontroller
{
    if (HUD != nil)
    {
        [HUD hide:YES];
        [HUD removeFromSuperview];
        HUD = nil;
    }
}


//网络测试  测试用
- (void)sethttpRequestFormat:(NSString *)address Tag:(int)tag Data:(NSData *)data
{
//    if (![HP_Tool isExistenceNetwork])
//    {
//        [self showMessage:@"未链接到网络,请检查网络设置"];
//        return;
//    }
    if (requestFormat != nil)
    {
       // [requestFormat release];
        requestFormat = nil;
    }
    requestFormat = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:address]];
    requestFormat.tag = tag;
    [requestFormat addRequestHeader:@"Content-Type" value:@"text/xml"];
    [requestFormat appendPostData:data];
    requestFormat.delegate = self;
    requestFormat.tag = tag;
    [requestFormat setRequestMethod:@"POST"];
    requestFormat.timeOutSeconds = 60;
    [requestFormat startAsynchronous];
}

- (NSString *)sendJson:(NSDictionary *)dictionary
{
    NSMutableString *jsonText = [[NSMutableString alloc] initWithString:@"{"];
    for (id key in dictionary)
    {
        [jsonText appendFormat:@"\"%@\":\"%@\",",key,[dictionary objectForKey:key]];
    }
    NSString *str = [jsonText substringToIndex:jsonText.length - 1];
    str = [NSString stringWithFormat:@"%@}",str];
    return str;
}

- (void)requestFinished:(ASIHTTPRequest *)request{
    
    NSString *responseStr = [request responseString];
    
    NSDictionary *jsonDict = [responseStr objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
    
    NSArray * dtaArr =jsonDict[@"data"];
}

@end
